package com.chenfan.mcn.rule;

import com.chenfan.mcn.clients.baseinfo.BaseCustomer;
import com.chenfan.mcn.clients.privilege.DepartmentVO;
import com.chenfan.mcn.clients.privilege.SUser;
import com.chenfan.mcn.model.CustomerTarget;
import com.chenfan.mcn.model.DepartmentTarget;
import com.chenfan.mcn.model.vo.QuarterIncomeContractVO;
import lombok.Data;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商务提成规则上下文
 *
 * @author wulg
 * @date 2021-09-25
 **/
@Data
public class BusinessCommissionRuleContext {

    private final String temporaryCode;

    private final Map<Long, BaseCustomer> customerMap;

    private final Map<Long, DepartmentVO> departmentVoMap;

    private final Map<Long, SUser> sUserMap;

    private final Map<String, DepartmentTarget> departmentTargetMap;

    private final Map<String, CustomerTarget> customerTargetMap;

    private final String separator = "_";

    private QuarterIncomeContractVO quarterIncomeContractVO;

    public BusinessCommissionRuleContext(final String temporaryCode, final Map<Long, BaseCustomer> customerMap, final Map<Long, DepartmentVO> departmentVoMap,
                                         final Map<Long, SUser> sUserMap, final List<DepartmentTarget> departmentTargets, final List<CustomerTarget> customerTargets) {
        this.temporaryCode = temporaryCode;
        this.customerMap = customerMap;
        this.departmentVoMap = departmentVoMap;
        this.sUserMap = sUserMap;

        if(CollectionUtils.isNotEmpty(departmentTargets)) {
            departmentTargetMap = new HashMap<>(departmentTargets.size());
            departmentTargets.forEach(target -> {
                departmentTargetMap.putIfAbsent(StringUtils.joinWith(separator, target.getQuarterCode(), target.getDepartmentId().toString()), target);
            });
            departmentTargets.clear();
        } else {
            departmentTargetMap = new HashMap<>(0);
        }

        if(CollectionUtils.isNotEmpty(customerTargets)) {
            customerTargetMap = new HashMap<>(customerTargets.size());
            customerTargets.forEach(target -> {
                customerTargetMap.putIfAbsent(StringUtils.joinWith(separator, target.getQuarterCode(), target.getTargetCustomerId().toString()), target);
            });
            customerTargets.clear();
        } else {
            customerTargetMap = new HashMap<>(0);
        }
    }

    public void clear() {
        if(MapUtils.isNotEmpty(customerMap)) {
            customerMap.clear();
        }
        if(MapUtils.isNotEmpty(departmentVoMap)) {
            departmentVoMap.clear();
        }
        if(MapUtils.isNotEmpty(sUserMap)) {
            sUserMap.clear();
        }
        if(MapUtils.isNotEmpty(departmentTargetMap)) {
            departmentTargetMap.clear();
        }
        if(MapUtils.isNotEmpty(customerTargetMap)) {
            customerTargetMap.clear();
        }
    }

    public BaseCustomer getBaseCustomer(Long customerId) {
        return customerMap.get(customerId);
    }

    public DepartmentVO getDepartmentVO(Long userId) {
        return departmentVoMap.get(userId);
    }

    public SUser getSuser(Long userId) {
        return sUserMap.get(userId);
    }

    public DepartmentTarget getDepartmentTarget(String quarterCode, Long departmentId) {
        return departmentTargetMap.get(StringUtils.joinWith( separator, quarterCode, departmentId.toString()));
    }

    public CustomerTarget getCustomerTarget(String quarterCode, Long customerId) {
        return customerTargetMap.get(StringUtils.joinWith( separator, quarterCode, customerId.toString()));
    }
}
